﻿using dz.u9.jky.schema.Common;
using System;
using System.IO;
using UFIDA.U9.Base.Attachment;
using UFIDA.U9.CS.Common.FileStorage;
using UFIDA.U9.CS.Unify.CommonBP;
using UFSoft.UBF.Business;
using UFSoft.UBF.Util.Context;


namespace dz.inner.jky.api.service
{
    internal class AttachmentService : BaseService
    {

        public U9CResult AddAttachment(DzAttachment dto)
        {
            return util.U9Invoke(__Add, dto);
        }

        private U9CResult __Add(DzAttachment dto)
        {
            U9CResult rtn = new U9CResult();

            Storage fileStorage = new Storage();
            foreach (var file in dto.Files)
            {
                string filehandler = fileStorage.SaveFile(file, File.ReadAllBytes(file));
                long fileID = 0;
                var eq = new EntityQuery(dto.EntityType);
                var ownerEntity = eq.FindByID(dto.Id);

                using (ISession session = Session.Open())
                {
                    UFIDA.U9.CS.Collaboration.Library.FileInfo fileInfo = UFIDA.U9.CS.Collaboration.Library.FileInfo.Create();

                    fileInfo.CreatedOn = DateTime.Now;
                    fileInfo.CreatedBy = PlatformContext.Current.UserID;
                    fileInfo.ModifiedBy = PlatformContext.Current.UserID;
                    fileInfo.ModifiedOn = DateTime.Now;
                    fileInfo.Title = file;
                    fileInfo.FileName = file;
                    fileInfo.FileExtension = System.IO.Path.GetExtension(file);
                    fileInfo.Handler = filehandler;
                    fileInfo.FileSize = new FileInfo(file).Length.ToString();
                    fileInfo.UploadDate = DateTime.Now;
                    fileInfo.UploadUser = UFIDA.U9.Base.UserRole.User.Finder.FindByID(long.Parse(PlatformContext.Current.UserID));
                    fileInfo.UploadDate = DateTime.Now;

                    fileInfo.OwnerEntity = (BusinessEntity)ownerEntity;
                    session.InList(fileInfo);
                    session.Commit();

                    fileID = fileInfo.ID;
                }

                using (ISession session = Session.Open())
                {
                    Attachment attachment = Attachment.Create();
                    attachment.CreatedOn = DateTime.Now;
                    attachment.CreatedBy = PlatformContext.Current.UserID;
                    attachment.ModifiedBy = PlatformContext.Current.UserID;
                    attachment.ModifiedOn = DateTime.Now;
                    attachment.Title = file;
                    attachment.URI = file;
                    attachment.FileHandler = filehandler;

                    attachment.EntityFullName = dto.EntityType;
                    attachment.EntityID = dto.Id;
                    attachment.AttachmentID = fileID;

                    session.InList(attachment);
                    session.Commit();
                }
            }
            rtn.IsSuccess = true;

            return rtn;
        }

        private long AddFile(DzAttachment dto)
        {
            FileInfoDTO file = new FileInfoDTO();

            FileInfo fi = new FileInfo(dto.Files[0]);
            file.FileName = fi.FullName;
            file.FileLength = fi.Length;
            file.FileExtension = fi.Extension;
            file.OwnerEntity_EntityType = dto.EntityType;
            file.OwnerEntity_EntityID = dto.Id;

            AddAttachmentBP bp = new AddAttachmentBP();

            bp.FileInfoDTO = file;

            return bp.Do();
        }
    }
}
